//
//  CMDBUserCenterProductionViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/5/4.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let CMDBUserCenterProductionCellID = "CMDBUserCenterProductionCell"

class CMDBUserCenterProductionViewController: CMDBUserBaseCenterViewController {
    
    var tableView : CMDBUserCenterProductionTableView!

    override func setup() {
        super.setup()
        
        buildTableView()
    }
}

extension CMDBUserCenterProductionViewController{
    
    override func buildTableView() {
        super.buildTableView()
        
        let contentH = CMDB_Screen_Height - KNavBarHeight - CMDB_Screen_Width * 0.5
        
        let contentFrame = CGRect(x: 0, y:  0, width: CMDB_Screen_Width, height: contentH)
        
        tableView = CMDBUserCenterProductionTableView(frame: contentFrame)
        
        tableView.separatorStyle = .None
        
        tableView.showsVerticalScrollIndicator = false
        
        tableView.mDelegate = self
        
        tableView.delegate = self
        
        tableView.backgroundColor = UIColor.whiteColor()
        
        tableView.scrollEnabled   = false
        
        view.addSubview(tableView)
        
    }
}

extension CMDBUserCenterProductionViewController{
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        
        let itemType = ItemClickType(string: itemClickType!)
        
        switch itemType {
        case .DeleteCell:
            
            var indexPathArr = [NSIndexPath]()
            
            indexPathArr.append(indexPath!)
            
            printLog(indexPath)
            
            tableView.deleteRowsAtIndexPaths(indexPathArr, withRowAnimation: .Fade)
            
            break
        default:
            print("default")
        }
    }
}

extension CMDBUserCenterProductionViewController:UITableViewDelegate{

    
//    func scrollViewDidScroll(scrollView: UIScrollView) {
//        
//        let pan = scrollView.panGestureRecognizer
//        
//        let velocity = pan.velocityInView(scrollView).y
//        
////        if velocity < -15 { //上滑
//        
//        if  scrollView.contentOffset.y < CMDB_Screen_Width * 0.5 + 44{
//            
//            containerVc?.topView.frame = CGRect(x: 0, y: -scrollView.contentOffset.y, width: CMDB_Screen_Width, height:CMDB_Screen_Width * 0.5)
//            
//            containerVc?.pageTitleView.y = CGRectGetMaxY(containerVc!.topView.frame)
//            
//            containerVc?.pageContentView.y = CGRectGetMaxY(containerVc!.pageTitleView.frame)
//            
//        }
//    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(CMDBUserCenterProductionCellID) as! CMDBUserCenterProductionCell
        
        cell.indexPath = indexPath
        
        cell.mDelegate = self
        
        return cell
        
        //        return dequeCellAndLoadContentFromAdapter(tableViewAdapterArray![indexPath.row] , indexPath: indexPath, tableView: self, controller: self.controller,cellReuseIdentifier : TopicCellID)
        
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        //        let viewModel = tableViewdataArray![indexPath.row] as! CMDBDynamicBean
        
        //return viewModel.cellHeight
        
        return 162
    }
}


extension CMDBUserCenterProductionViewController{
    
    func onHeaderRefresh() {
        
//        printLog("onHeaderRefresh")
//        dispatch_queue_t()
//        dispatch_async(dispatch_get_main_queue()) {
//            
//        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
            
            self.tableView.mj_header.endRefreshing()
            
            self.tableView.reloadData()
            
        }
        
    }
    
    func onFooterRefresh() {
        printLog("onFooterRefresh")
    }
    
}